<template>
  <router-view v-slot="{ Component, route }">
    <transition appear name="fade-transform" mode="out-in">
      <keep-alive :include="keepAliveList">
        <component :is="Component" :key="route.path" v-if="refreshFlag" />
      </keep-alive>
    </transition>
  </router-view>
</template>

<script setup>
import { nextTick, ref } from 'vue'
import { storeToRefs } from 'pinia'
import { useMenuStore } from '@/stores/menu'
import { useEventBus } from '@vueuse/core'
import { useTabbar } from '@/hooks/useTabbar'

const menuStore = useMenuStore()
const { keepAliveList } = storeToRefs(menuStore)

const refreshFlag = ref(true)
const e_refresh_page = useEventBus('E_REFRESH_PAGE')
e_refresh_page.on((tab) => {
  setTimeout(() => {
    if (tab.meta.keepAlive) useTabbar().removeKeepAlive(tab)
    refreshFlag.value = false
    nextTick(() => {
      if (tab.meta.keepAlive) useTabbar().addKeepAlive(tab)
      refreshFlag.value = true
    })
  })
})
</script>

<style lang="scss" scoped>
/* fade-transform */
.fade-transform-leave-active,
.fade-transform-enter-active {
  transition: all 0.2s;
}
.fade-transform-enter-from {
  opacity: 0;
  transition: all 0.2s;
  // transform: translateX(-30px);
}
.fade-transform-leave-to {
  opacity: 0;
  transition: all 0.2s;
  // transform: translateX(30px);
}
</style>
